import chapterApi from '@/api/chapter'

const state = {
  chapters: []
}

const getters = {
  chapterEnumFormat: (state) => (key) => {
    for (let item of state.chapters) {
      if (item.chapterId === key) {
        return item.chapterName
      }
    }
    return null
  }
}

// actions
const actions = {
  initChapter ({ commit }, level) {
    chapterApi.list(level).then(re => {
      commit('setChapters', re.response)
    })
  }
}

// mutations
const mutations = {
  setChapters: (state, chapters) => {
    state.chapters = chapters
  }
}

export default {
  namespaced: true,
  state,
  getters,
  actions,
  mutations
}
